import {uploadFile as upFile, uploadImage as upImage} from "./Request";

function wrapperResData(data) {
    let resList = [], urlList = [], url = '', pathList = [], path = '';
    if (data) {
        resList = data;
        data.forEach((v, i) => {
            urlList.push(v.url);
            pathList.push(v.path);
        });
        url = urlList.join(",");
        path = pathList.join(",");
    }
    return {resList, urlList, url, pathList, path};
}

//上传图片
/**
 *
 * @param path 路径数组
 * @returns {Promise<*>}
 * {
        code: 0,
        data:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        }
    }
 */
export async function uploadImage({images, imageBase64s}, success) {
    if ((!images || images.length === 0) && (!imageBase64s || imageBase64s.length === 0)) {
        return {code: 0, data: {}};
    }
    let resList = [];
    try {
        if (images && images.length > 0) {
            for (let i = 0; i < images.length; i++) {
                Toast.loading(`上传第${i + 1}张图片中...`, 0, null, true);
                let result = await upImage({file: images[i]});
                if (result.code !== 0) {
                    Toast.fail(result.msg || result.message, 3);
                    return result;
                }
                resList.push(result.data);
                if (success) {
                    success({data: result.data, index: i, type: 'image'});
                }
            }
        }

        if (imageBase64s && imageBase64s.length > 0) {
            for (let i = 0; i < imageBase64s.length; i++) {
                Toast.loading(`上传第${i + 1}张图片中...`, 0, null, true);
                let result = await upImage({base64: imageBase64s[i]});
                if (result.code !== 0) {
                    Toast.fail(result.msg || result.message, 3);
                    return result;
                }
                resList.push(result.data);
                if (success) {
                    success({data: result.data, index: i, type: 'imageBase64'});
                }
            }
        }
        Toast.hide();
    } catch (e) {
        Toast.fail('上传失败', 3);
        return {code: -1, msg: '上传失败', message: '上传失败'};
    }
    let data = wrapperResData(resList);
    return {code: 0, data};
}

//上传文件
/**
 *
 * @param path //路径数组
 * @returns {Promise<*>}
 *  {
        code: 0,
        data:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        }
    }
 *
 */
export async function uploadFile({files, type = 'file'}, success) {
    if (!files || files.length === 0) {
        return {code: 0, data: {}};
    }
    let resList = [];
    try {
        let typeName = '文件';
        if (type === 'video') {
            typeName = '视频';
        } else if (type === 'audio') {
            typeName = '音频';
        }
        for (let i = 0; i < files.length; i++) {
            Toast.loading(`上传第${i + 1}个${typeName}中...`, 0, null, true);
            let result = await upFile({file: files[i]});
            if (result.code !== 0) {
                Toast.fail(result.msg || result.message, 3);
                return result;
            }
            resList.push(result.data);
            if (success) {
                success({data: result.data, index: i, type});
            }
        }
        Toast.hide();
    } catch (e) {
        Toast.fail('上传失败', 3);
        return {code: -1, msg: '上传失败', message: '上传失败'};
    }
    let data = wrapperResData(resList);
    return {code: 0, data};
}

//上传素材
/**
 *
 * @param imgPath 图片路径数组
 * @param audioPath 音频路径数组
 * @param videoPath 视频路径数组
 * @param filePath 文件路径数组
 * @returns {Promise<*>}
 * {
        code: 0,
        imgData:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        },
        audioData:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        },
        videoData:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        },
        fileData:{
            resList:[
                {
                   url:'xxx',
                   path:'xxx',
                   size:12,
                   mediaType:'jpg',
                   name:'xxx'
                }
            ],
            urlList:['xxx','xxx'],
            url:'xx,xxx'
        }
    }
 */
export async function uploadMaterial({imgPath, audioPath, videoPath, filePath}, success) {
    let imgData, audioData, videoData, fileData;
    if (imgPath && imgPath.length > 0) {
        let result = await uploadImage({images: imgPath}, success);
        if (result.code !== 0) {
            return {code: -1, msg: result.msg || result.message};
        }
        imgData = result.data;
    }

    if (audioPath && audioPath.length > 0) {
        let result = await uploadFile({files: audioPath, type: 'audio'}, success);
        if (result.code !== 0) {
            return {code: -1, msg: result.msg || result.message};
        }
        audioData = result.data;
    }

    if (videoPath && videoPath.length > 0) {
        let result = await uploadFile({files: videoPath, type: 'video'}, success);
        if (result.code !== 0) {
            return {code: -1, msg: result.msg || result.message};
        }
        videoData = result.data;
    }

    if (filePath && filePath.length > 0) {
        let result = await uploadFile({files: filePath, type: 'file'}, success);
        if (result.code !== 0) {
            return {code: -1, msg: result.msg || result.message};
        }
        fileData = result.data;
    }
    return {
        code: 0,
        imgData, audioData, videoData, fileData
    }
}